package no.ntnu.fp.model;

import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import no.ntnu.fp.utils.Participant;
import no.ntnu.fp.utils.XmlParser;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Meeting extends Appointment{
	private boolean isCanceled;
	private List<Participant> participants;

	public Meeting(long id, String place, String title, String description,
			MeetingRoom room, Employee emp, Time startTime, Time endTime,
			Date date, boolean isCanceled, List<Participant> participants) {
		super(id, place, title, description, room, emp, startTime, endTime,
				date);
		this.isCanceled = isCanceled;
		this.participants = new ArrayList<Participant>(participants);
	}

	public Meeting(Element root) {
		super(root);
	}
	
	public boolean isCancelled() {
		return isCanceled;
	}

	public List<Participant> getParticipants() {
		return new ArrayList<Participant>(participants);
	}

	@Override
	protected void initializeFromXml(Element rootNode) {
		this.id = Long.parseLong(XmlParser.getTextValue(rootNode, "id"));
		this.title = XmlParser.getTextValue(rootNode, "title");
		this.title = (title == null ? "" : title);
		this.emp = new Employee((Element)rootNode.getElementsByTagName("employee").item(0));
		this.date = Date.valueOf(XmlParser.getTextValue(rootNode, "date"));
		this.startTime = Time.valueOf(XmlParser.getTextValue(rootNode, "startTime"));
		this.endTime = Time.valueOf(XmlParser.getTextValue(rootNode, "endTime"));
		this.description = XmlParser.getTextValue(rootNode, "description");
		this.description = (description == null ? "" : description);
		this.place = XmlParser.getTextValue(rootNode, "place");
		
		if (this.place == null) {
			Element meetingRoomElement 
				= (Element)rootNode.getElementsByTagName("meetingroom").item(0);

			int idTagCount = meetingRoomElement.getElementsByTagName("id").getLength();
			
			if (meetingRoomElement.hasChildNodes() && idTagCount == 1) {
				
				this.room = new MeetingRoom(meetingRoomElement);
			}
		}
		
		this.isCanceled = Boolean.parseBoolean(XmlParser.getTextValue(rootNode, "isCanceled"));
		
		if (this.participants == null) {
			this.participants = new ArrayList<Participant>();
		} else {
			this.participants.clear();
		}
		
		NodeList participants = rootNode.getElementsByTagName("participants");
		NodeList participantList = ((Element)participants.item(0)).getChildNodes();
		for(int i = 0; i < participantList.getLength(); i++){
			this.participants.add(new Participant((Element)(participantList.item(i))));
		}
	}

	@Override
	public Element toXml(Document dom) {
		Element rootElement = dom.createElement("meeting");
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "id", id + ""));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "title", title));
		rootElement.appendChild(emp.toXml(dom));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "date", date.toString()));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "startTime", startTime.toString()));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "endTime", endTime.toString()));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "description", description));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "place", place));

		if (room != null) {
			rootElement.appendChild(room.toXml(dom));
		} else {
			rootElement.appendChild(XmlParser.createElementWithValue(dom, "meetingroom", "null"));
		}
		
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "isCanceled", this.isCanceled + ""));
		
		Element participants = dom.createElement("participants");
		for(Participant participant: this.participants){
			participants.appendChild(participant.toXml(dom));
		}
		rootElement.appendChild(participants);
		
		return rootElement;
	}

}
